/*
 * @(#)org.example.netty.groupchat 2020/4/2/0002
 *
 * Copy Right@ 人人译视界
 */


package org.example.netty.groupchat;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.util.Scanner;

/**
 * <pre>
 * @author tuwei
 *
 * @version 0.9
 *
 * 修改版本: 0.9
 * 修改日期: 2020/4/2/0002
 * 修改人 :  tuwei
 * 修改说明: 初步完成
 * 复审人 ：
 * </pre>
 */
public class GroupChatClient {
	private final String host;
	private final Integer port;

	public GroupChatClient(String host, Integer port) {
		this.host = host;
		this.port = port;
	}

	public void run() throws InterruptedException {
		NioEventLoopGroup eventExecutors = new NioEventLoopGroup();
		try {
			Bootstrap bootstrap = new Bootstrap();
			bootstrap.group(eventExecutors)
					.channel(NioSocketChannel.class)
					.handler(new ChannelInitializer<SocketChannel>() {
						@Override
						protected void initChannel(SocketChannel socketChannel) throws Exception {
							ChannelPipeline pipeline = socketChannel.pipeline();
							pipeline.addLast("decoder", new StringDecoder());
							pipeline.addLast("encoder", new StringEncoder());
							pipeline.addLast(new GroupChatClientHandler());
						}
					});
			ChannelFuture channelFuture = bootstrap.connect(host, port).sync();
			Channel channel = channelFuture.channel();
			System.out.println("------" + channel.localAddress() + "----------");
			// 客户端输入信息
			Scanner scanner = new Scanner(System.in);
			while (scanner.hasNextLine()) {
				String s = scanner.nextLine();
				channel.writeAndFlush(s);
			}
		} finally {
			eventExecutors.shutdownGracefully();
		}
	}

	public static void main(String[] args) throws InterruptedException {
		new GroupChatClient("127.0.0.1", 7000).run();
	}
}
